<template>
  <el-card
    ><el-row :gutter="20" class="header"
      ><el-col :span="7"
        ><el-input
          v-model="queryForm.name"
          placeholder="请输入学科id"
          tabindex="2"
          clearable
      /></el-col>
      <el-button type="primary" :icon="Search" @click="regetuser"
        >Search</el-button
      ><el-button type="primary" @click="handleadduser"
        >发送消息</el-button
      ></el-row
    ><el-table :data="tableData" stripe style="width: 100%">
      <el-table-column
        :prop="item.prop"
        :label="item.label"
        :width="item.width"
        v-for="(item, index) in options"
        :key="index"
      >
        <template v-slot="{ row }" v-if="item.prop === 'status'"
          ><el-switch v-model="row.statusbool" @change="changestatus(row)"
        /></template> </el-table-column
    ></el-table>
    <el-pagination
      v-model:currentPage="queryForm.pageIndex"
      v-model:page-size="queryForm.pageSize"
      :page-sizes="[5, 10, 20, 50]"
      :small="small"
      :disabled="disabled"
      :background="background"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
  /></el-card>
  <Edit v-model="editVisible" @inituserlist="inigetuser" :rowvalue="rowvalue" />
</template>

<script setup>
import { reactive, ref } from 'vue'
import { Search } from '@element-plus/icons-vue'
import { getmessagelists } from '@/api/message'
import {
  getsubjects,
  getsubject,
  getsubjectfromid,
  deletesubject
} from '@/api/subject'
import { options } from './options'
import { ElMessage } from 'element-plus'
import Dialog from './components/dialog.vue'
import Edit from './components/edit.vue'
import router from '@/router'
const tableData = ref([])

const queryForm = ref({
  name: 2,
  pageIndex: 1,
  pageSize: 10
})

const total = ref(10)
const dialogVisible = ref(false) //   添加用户是否弹出
const editVisible = ref(false) //   编辑是否弹出
const rowvalue = ref({}) //  编辑信息的原信息
//  更新table
const inigetuser = async () => {
  const res = await getmessagelists(queryForm.value)
  console.log(res)
  total.value = res.data.data.total
  tableData.value = res.data.data.list
}
inigetuser()

const handleSizeChange = (pageSize) => {
  queryForm.value.pageIndex = 1
  queryForm.value.pageSize = pageSize
  inigetuser()
}

const handleCurrentChange = (pagenum) => {
  queryForm.value.pageIndex = pagenum
  inigetuser()
}
//  通过id检索
const regetuser = async () => {
  const res = await getsubjectfromid(queryForm.value.name)
  console.log(res.data.data)
  let temp = ref([])
  tableData.value = temp.value
  tableData.value[0] = res.data.data
  tableData.value[0].level_name = tableData.value[0].levelName
  total.value = 1
}
//  添加学科
const handleadduser = () => {
  router.replace('/admin/editmessage')
}
// 编辑学科
const edituser = (row) => {
  rowvalue.value = JSON.parse(JSON.stringify(row))
  editVisible.value = true
}

// 删除学科
const clickdeleteuser = (info) => {
  deletesubject(info.id)
  ElMessage({
    showClose: true,
    message: '成功删除学科',
    type: 'success'
  })
  inigetuser()
}
</script>

<style lang="scss" scoped></style>
